/**
 * Created by a1 on 16/5/3.
 */

var url = "RiskQuestion"
var keys
var titles
var params = {}

$(function(){
    init()
})

function init() {
    $("input#btnCommit").click(function() {
        var length = keys.length
        var invalidIndex = -1
        for(var i = 0; i < length; ++i) {
            var key = keys[i]
            var nodeTitle = $("#"+"title"+i)
            var nodeInput = $("#"+key)
            if(nodeInput.attr("type") == "number") {
                params[key] = nodeInput.val()
            }
            if(params[key] == null || params[key].length == 0) {
                nodeTitle.attr("error", "true")
                invalidIndex = i
                break
            }else {
                nodeTitle.attr("error", "")
            }
        }
        if(invalidIndex == -1) {
            //参数正确
            alert("params = " + JSON.stringify(params))
        }else {
            alert(titles[invalidIndex] + "不能为空!")
            scrollTo(nodeTitle, 200)
        }
    })

    //滚动至指定元素
    function scrollTo(element, duration) {
        var offset = element.offset()  //得到pos这个div层的offset，包含两个值，top和left
        $("body,html").animate({
            scrollTop:offset.top  //让body的scrollTop等于pos的top，就实现了滚动
        },duration);
    }

    $.getJSON(url, function(json, status) {
        keys = []
        titles = []
        //alert(JSON.stringify(json))
        var length = json.length
        var parent = $("div#container")
        for(var i = 0; i < length; ++i) {
            var question = json[i]
            for (var key in question) {
                keys[i] = key
                var value = question[key]
                addElement(i, parent, value)
                break
            }
        }
        //alert("key = " + JSON.stringify(keys))
    })
}

function addElement(index, parent, json) {
    for(var key in json) {
        titles[index] = key
        var node = $("<div class='question'></div>")
        var id = "title"+index
        var p = $("<p id='"+id+"' class='title'></p>")
        var title = index + 1 + ". " + key
        p.html(title)
        node.append(p)
        parent.append(node)
        addAnswers(index, node, json[key])
        break
    }
}

function addAnswers(index, parent, json) {
    if($.trim(JSON.stringify(json)).length <= 2) {
        //alert("is input")
        var node = $("<div class='text'><p><input id='"+keys[index]+"' type='number'/></p></div>")
        parent.append(node)
    }else {
        //alert("is json")
        var node = $("<div id='"+keys[index]+"'</div>")
        for(var key in json) {
            //alert("value = " + json[key])
            var item = $("<div class='item' value='"+key+"'>"+json[key]+"</div>")
            var checkBox = $("<svg class='checkBox' position='right' viewBox='0 0 100 100'><use xlink:href='#checkBox'></use></svg>")
            item.append(checkBox)
            node.append(item)
            item.click(function() {
                var children = $("#"+keys[index]).children()
                for(var i = 0; i < children.length; ++i) {
                    $(children[i]).find(".checkBox").attr("checked", null)
                }
                //value
                var checkBox = $(this).find(".checkBox")
                checkBox.attr("checked", "checked")
                var value =$(this).attr("value")
                params[keys[index]] = value
            })
        }
        parent.append(node)
    }
}

//function init() {
//    alert("init")
//    $.get(url, function(data, status) {
//        alert("data = " + data)
//        keys = []
//        var json = eval('('+data+')')
//        alert(JSON.stringify(json))
//        var length = json.length
//        for(var i = 0; i < length; ++i) {
//            var question = json[i]
//            alert("i = " + question.first())
//
//            //$.each(question, function(key) {
//            //    keys[i] = key
//                //$.each(question[key], function(key) {
//                //    alert("title = " + key)
//                //    var answers = value[key]
//                //    alert("answers = " + answers)
//                //    if(answers instanceof String) {
//                //
//                //    }else {
//                //        alert("Not input")
//                //    }
//                //})
//                //break
//            //})
//        }
//    })
//}